package com.stu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.entity.Student;
import com.stu.entity.SysUser;
import com.stu.service.StudentService;
import com.stu.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@CrossOrigin
@Slf4j
@Api(value = "学生管理接口")
public class StudentController {

    @Autowired
    SysUserService userService;

    @Autowired
    StudentService studentService;

    @GetMapping("/student/stu_login")
    public Map<String, Object> login(SysUser sysUser) {
        System.out.println("jinru");
        log.info("11111-------------");
        Map<String, Object> map = new HashMap<>();
        SysUser user = userService.login(sysUser);
        if (null != user) {
            map.put("成功", user);
        } else {
            map.put("失败", null);
        }

        return map;
    }

    @ApiOperation(value = "查询全部")
    @GetMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<Student> students = studentService.list();
        if (students.size() > 0) {
            map.put("message", "查询成功");
            map.put("data", students);
            map.put("code", "200");
        } else {
            map.put("message", "查询失败");
            map.put("data", null);
            map.put("code", "500");
        }

        return map;
    }

    @ApiOperation(value = "分页查询学生表")
    @GetMapping("/page/{page}/{limit}")
    public Map<String, Object> page(@PathVariable int page, @PathVariable int limit, String name) {
        log.info("page:{} limit:{} name:{}", page, limit, name);
        Map<String, Object> map = new HashMap<>();
        Page pageInfo = studentService.page(page, limit, name);
        if (null != pageInfo) {
            map.put("message", "查询成功");
            map.put("data", pageInfo);
            map.put("code", "200");
        } else {
            map.put("message", "查询失败");
            map.put("data", null);
            map.put("code", "500");
        }

        return map;
    }

    @ApiOperation(value = "分页查询外带专业学院")
    @GetMapping("/getpage/{page}/{limit}")
    public Map<String, Object> getpage(@PathVariable int page, @PathVariable int limit) {
        log.info("page:{} limit:{}", page, limit);
        Map<String, Object> map = new HashMap<>();
        List<Student> page1 = studentService.getPage(page, limit);
        if (null != page1) {
            map.put("message", "查询成功");
            map.put("data", page1);
            map.put("code", "200");
        } else {
            map.put("message", "查询失败");
            map.put("data", null);
            map.put("code", "500");
        }

        return map;
    }

    //返回学生表，专业表，学院表总条数----统计表中行
    @ApiOperation(value = "返回学生表，专业表，学院表总条数")
    @GetMapping("/getCount")
    public Map<String, Object> getCount() {
        Map<String, Object> map = new HashMap<>();
        Integer[] allRow = studentService.getAllRow();
        if (allRow.length>0) {
            map.put("message", "统计成功");
            map.put("data", allRow);
            map.put("code", "200");
        } else {
            map.put("message", "统计失败");
            map.put("data", null);
            map.put("code", "500");
        }
        return map;
        }


}
